package com.hushui.mall.commodity.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.hushui.common.utils.BeanUtilCopy;
import com.hushui.mall.commodity.entity.AttrAttrgroupRelationEntity;
import com.hushui.mall.commodity.entity.AttrGroupEntity;
import com.hushui.mall.commodity.entity.CategoryEntity;
import com.hushui.mall.commodity.entity.vo.AttrVo;
import com.hushui.mall.commodity.entity.vo.AttrgcVo;
import com.hushui.mall.commodity.service.AttrAttrgroupRelationService;
import com.hushui.mall.commodity.service.AttrGroupService;
import com.hushui.mall.commodity.service.CategoryService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hushui.common.utils.PageUtils;
import com.hushui.common.utils.Query;

import com.hushui.mall.commodity.dao.AttrDao;
import com.hushui.mall.commodity.entity.AttrEntity;
import com.hushui.mall.commodity.service.AttrService;
import org.springframework.util.StringUtils;


@Service("attrService")
public class AttrServiceImpl extends ServiceImpl<AttrDao, AttrEntity> implements AttrService {
    @Autowired
    private CategoryService service;
    @Autowired
    private AttrAttrgroupRelationService attrAttrgroupRelationService;
    @Autowired
    private AttrGroupService attrGroupService;
    @Autowired
    private CategoryService categoryService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrEntity> page = this.page(
                new Query<AttrEntity>().getPage(params),
                new QueryWrapper<AttrEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPage(Map<String, Object> params, Long catelogId) {
        if(catelogId == 0){
            IPage<AttrEntity> page = this.page(new Query<AttrEntity>().getPage(params), new QueryWrapper<>());
            return new PageUtils(page);
        }else {
            String key = (String)params.get("key");
            QueryWrapper<AttrEntity> wrapper = new QueryWrapper<>();
            if (!StringUtils.isEmpty(key)){
                wrapper.and((obj)->{
                    obj.eq("attr_id",key).or().like("attr_name",key).or().like("value_select",key);
                });
            }
            IPage<AttrEntity> page=this.page(new Query<AttrEntity>().getPage(params),wrapper);
            return new PageUtils(page);
        }
    }

    @Override
    public PageUtils queryVoPage(Map<String, Object> params, Long catelogId) {
        IPage<AttrEntity> page = this.page(new Query<AttrEntity>().getPage(params), new QueryWrapper<>());
        IPage<AttrgcVo> convertPage = page.convert(result -> {
            AttrgcVo vo = new AttrgcVo();
            BeanUtils.copyProperties(result,vo);
            QueryWrapper<AttrAttrgroupRelationEntity> wrapper = new QueryWrapper<>();
            wrapper.eq("attr_id",vo.getAttrId());
            Long attrGroupId = attrAttrgroupRelationService.getOne(wrapper).getAttrGroupId();
            QueryWrapper<AttrGroupEntity> wrapper1 = new QueryWrapper<>();
            wrapper.eq("attr_group_id",attrGroupId);
            vo.setGroupName(attrGroupService.getOne(wrapper1).getAttrGroupName());
            QueryWrapper<CategoryEntity> wrapper2 = new QueryWrapper<>();
            wrapper.eq("cat_id",vo.getCatelogId());
            vo.setCatelogName(categoryService.getOne(wrapper2).getName());
            return vo;
        });
        if(catelogId == 0){
            return new PageUtils(convertPage);
        }else {

            String key = (String)params.get("key");
            List<AttrgcVo> records = convertPage.getRecords();
            Iterator<AttrgcVo> iterator = records.iterator();
            while (iterator .hasNext()) {
                AttrgcVo vo = iterator .next();
                if (!(vo.getAttrId().toString().equals(key) ||vo.getAttrName().contains(key) || vo.getValueSelect().contains(key) || vo.getAttrId().toString().equals(key) || vo.getGroupName().contains(key) || vo.getCatelogName().contains(key))){
                    iterator.remove();//这里要使用Iterator的remove方法移除当前对象，如果使用List的remove方法，则同样会出现ConcurrentModificationException
                    page.setTotal(page.getTotal()-1l);
                }
            }
            return new PageUtils(page);
        }
    }

    @Override
    public ArrayList<Long> getPath(Long catelogId) {
        QueryWrapper<CategoryEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("cat_id",catelogId);
        CategoryEntity one = service.getOne(wrapper);
        ArrayList<Long> longs = new ArrayList<>();
        longs.add(catelogId);
        while (one.getCatLevel() != 1){

            one = service.getById(one.getParentCid());
            longs.add(0,one.getCatId());
        }
        return longs;
    }
    @Override
    public List<Long> selectSearchAttrs(List<Long> attrIds) {

        List<Long> searchAttrIds = this.baseMapper.selectSearchAttrIds(attrIds);

        return searchAttrIds;
    }

}